package com.github.edu.client.common.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.github.admin.edu.assembly.common.entity.JsonArray;
import com.github.admin.edu.assembly.common.entity.JsonEntity;
import com.github.admin.edu.assembly.common.util.JsonUtils;
import com.github.admin.edu.assembly.page.entity.Pager;
import com.github.admin.edu.oauth.server.api.entity.TSequence;
import com.github.edu.client.common.domain.TSequenceDomain;
import com.github.edu.client.common.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Create by IntelliJ IDEA
 * 用户：王建
 * 日期：2018-7-25
 */
public abstract class BaseAbstractService {

    @Autowired
    private TSequenceDomain domain;

    public Long getId(String obj){

        String code=domain.getObjectId(obj);

        if(!StringUtils.isEmpty(code)){
            JsonEntity<TSequence> jsonEntity=JsonUtils.toCollection(code, new TypeReference<JsonEntity<TSequence>>() {
            });
            if(null!=jsonEntity){
                TSequence sequence=jsonEntity.getData();
                if(null!=sequence){
                    return sequence.getId();
                }
            }
        }

        return (long)0;
    }

    public <T>String jsonEntity(T t, Pager pager){
        JsonEntity<T> jsonEntity=new JsonEntity<>();
        jsonEntity.setData(t);
        jsonEntity.setPager(pager);
        return JsonUtils.toJson(jsonEntity);
    }


    public int deleteValue(String code){
        if(!StringUtils.isEmpty(code)){
            Map<String,Object> map=JsonUtils.json2map(code);
            if(null!=map)
                return (int)map.get("value");
        }
        return 0;
    }
}
